﻿using UnityEngine;

/// <summary>
/// 背景音乐管理
/// </summary>
public class BGSound : ISound
{
    private AudioSource audioSource;
    private int curID = -1;

    public BGSound()
    {
        audioSource = AllUpdate.UIRoot.AddComponent<AudioSource>();
        audioSource.loop = true; Mute = false;
    }

    public override bool Mute
    {
        get
        {
            return base.Mute;
        }
        set
        {
            base.Mute = value;
            audioSource.mute = value;
        }
    }

    public override AudioSource Play(Table_SoundTempItem soundTemp, AudioClip clip)
    {
        if (curID != soundTemp.Id)
        {
            curID = soundTemp.Id;
            audioSource.Stop();
            audioSource.clip = clip;
            audioSource.volume = soundTemp.Volume;
            audioSource.pitch = 1; RePlay();
        }
        else if (!IsPlaying(curID))
        {
            RePlay();
        }
        return audioSource;
    }

    public override void Pause()
    {
        if (audioSource.clip != null)
        {
            audioSource.Pause();
        }
    }

    public override void RePlay()
    {
        if (audioSource.clip != null)
        {
            audioSource.Play();
        }
    }

    public override void Stop()
    {
        audioSource.Stop();
    }

    public override void StopById(int id)
    {
        audioSource.Stop();
    }

    public override void Update() { }

    public override bool IsPlaying(int id)
    {
        return audioSource.isPlaying;
    }

    public override void SetPitch(int id, float pitch)
    {
        if (audioSource.clip != null)
        {
            audioSource.pitch = pitch;
        }
    }
}